package com.vm.dao.system;


import java.util.List;
import java.util.Map;
import com.vm.dao.BaseDAOImpl;
import com.vm.entity.MenuButton;
import com.vm.entity.Role;

public class RoleDAOImpl extends BaseDAOImpl<Role, String> implements IRole {
	
	
	public RoleDAOImpl() {
		setEntityClass(Role.class);
	}

	public List<Map<String, Object>> findAllMenu(){
		String hql = "select id as id,parentid as  pId,name as name,prmssCd as value,buttonGrp as buttonGrp FROM Menu where vldMrk=1" ;
		return super.findBySqlALL(hql, null);
	}
	public List<Map<String, Object>> findallButtens(){
		String hql = "select prmssVal as value,name as name FROM GrpButton" ;
		return super.findBySqlALL(hql, null);
	}
	
	public void updateGrp(String roleId,Map<String,String> map,String menuGrps){
		Role role = this.findById(roleId);
		if("".equals(menuGrps) || menuGrps==null){//没有选中任何菜单的情况下 应删除按钮表的所有内容
			role.setPrmssGrp("");
			String hql = "delete from MenuButton where rlId=?";
			this.update(role);
			this.deleteByHql(hql, new Object[]{roleId});
		}else{
			String[] meGrps = menuGrps.split(",");
			String prmssGrp = this.getButtonValues(meGrps);
			role.setPrmssGrp(prmssGrp);
			this.update(role);
			if(map != null && map.size()>0){
				String hql = "delete from MenuButton where rlId=?";
				this.deleteByHql(hql, new Object[]{roleId});
				for(int i=0;i<meGrps.length;i++){
					if(map.get(meGrps[i])!=null && !"".equals(map.get(meGrps[i]))){
						MenuButton mebt = new MenuButton();
						mebt.setRlId(roleId);
						mebt.setMenuId(meGrps[i]);
						mebt.setPrmssVal(Long.parseLong(map.get(meGrps[i])));
						super.getHibernateTemplate().save(mebt);;
					}
				}
			}
		}
	}
}
